package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrderService;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 下单
     */
    @PostMapping("/submit")
    public R submit(@RequestBody Orders orders) {
        orderService.submit(orders);
        return R.success("下单成功");
    }

    @GetMapping("/userPage")
    public R userPage(int page, int pageSize) {
        log.info("controller接收到分页查询订单数据请求 page:{},pageSize:{}", page, pageSize);
        Page<OrdersDto> pages = orderService.listByPageAndCondition(page, pageSize);
        return R.success(pages);
    }

    /*订单分页*/
    @GetMapping("page")
    public R<Page<OrdersDto>> listByPage(
            Integer page,
            Integer pageSize,
            Long number,
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime,
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        log.info("controller接收到订单分页请求 page{},pageSize{},number{},beginTime{},endTime{}",
                page, pageSize, number, beginTime, endTime);
        Page<OrdersDto> ordersDtoPage = orderService.pageByCondition(page, pageSize, number, beginTime, endTime);
        List<OrdersDto> records = ordersDtoPage.getRecords();
        List<OrdersDto> collect = records.stream().peek(o -> {
            if (o.getUserName() == null || o.getUserName().equals("")) {
                o.setUserName("用户暂未设置昵称");
            }
        }).collect(Collectors.toList());
        ordersDtoPage.setRecords(collect);
        return R.success(ordersDtoPage);
    }

    /*修改单个订单状态 派送/完成*/
    @PutMapping
    public R<Object> updateStatusOne(@RequestBody Orders orders) {
        Long id = orders.getId();
        Integer status = orders.getStatus();
        if (status == 3) {
            log.info("controller接收到派送订单请求 id:{}", id);
        }
        if (status == 4) {
            log.info("controller接收到完成订单请求 id:{}", id);
        }
        orderService.updateById(orders);
        return R.success(null);
    }

    @Autowired
    OrderDetailService orderDetailService;
    @Autowired
    ShoppingCartController shoppingCartController;

    @PostMapping("again")
    public R<Object> saveOrdersByCondition(@RequestBody Orders orders) {
        Long id = orders.getId();
        log.info("controller接收到用户再来一单 请求 订单id:{}", id);
        // 已获得订单id 只需要将对应订单的信息转入购物车即可 用户id从BaseContext中获取
        // 从订单id中获取该订单下有多少菜 即订单明细
        LambdaQueryWrapper<OrderDetail> lqw = new LambdaQueryWrapper<>();
        lqw.eq(OrderDetail::getOrderId, id);
        List<OrderDetail> list = orderDetailService.list(lqw);
        // list中有每个订单明细
        Long currentId = BaseContext.getCurrentId();
        for (OrderDetail orderDetail : list) {
            ShoppingCart shoppingCart = new ShoppingCart();
            BeanUtils.copyProperties(orderDetail, shoppingCart);
            shoppingCart.setUserId(currentId);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartController.add(shoppingCart);
        }
        return R.success(null);
    }
}